<?php
/**
 * Home_Controller
 * - Controller for module page Home
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller

class Scategory_Controller extends ControllerAdmin {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->menuDanhMuc = true;
    	$this->menuSCate = true;
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = '';
        $this->tpl_title_list = '';
        $this->flagAdd = false;
        $this->currPage = '';
		$this->numberPage = '';
		$this->strPaging = '';
        
    	if (!$this->loginFlag || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        
        $ojbCategory = new SanphamCategory();
    	$ojbSCategory = new SanphamSCategory();
    	$ojbSanPham = new MSanpham();
    	
    	$dataGet = FunctionCommon::getGet(array('iddm', 'danhmucid'));
        $this->iddm = !empty($dataGet['iddm']) ? $dataGet['iddm'] : '';
     	$danhmucid = $dataGet['danhmucid'];
        if ($danhmucid != ''){
        	Session::Set("danhmucid", $danhmucid);
        } else {
        	$danhmucid = Session::Get("danhmucid");
        }
        $this->danhmucid = $danhmucid;
        $this->subiddm = $ojbSCategory->getIDDMC($this->danhmucid, $this->iddm);

        if ($this->isPost()) {
        	$dataPost = FunctionCommon::getPost(array('itemID', 'mode', 'type', 'name', 'checkdel'));
        	if (Session::Get("CATE_SP") == 'true') {
	        	// Delete by id
				if ($dataPost['mode'] == 'delete') {
				     $dataSanpham = $ojbSanPham->getByPrimaryKey($dataPost['itemID']);
				     $ojbSanPham->delete(array('id' => $dataPost['itemID']));
				     FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
				     FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
				}
				        
				// Delete all by array id
				if ($dataPost['mode'] == 'deleteall') {
				    if (count ($dataPost['checkdel']) > 0) {
				    	foreach ($dataPost['checkdel'] as $aP) {
				    		$dataSanpham = $ojbSanPham->getByPrimaryKey($aP);
				        	$ojbSanPham->delete(array('id' => $aP));
				        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
				        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
				    	}
				    }
				}
				
		    	// Up
		        if ($dataPost['mode'] == 'up') {
		        	$id = $dataPost['itemID'];
		        	$seq = $dataPost['type'];
		        	$seqMin = $ojbSanPham->getMinMax(array("seq"=>array("math"=>">", "value"=>$seq), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm)));
		        	$idMin = $ojbSanPham->getIdBySeq($seqMin);
		        	
		        	$ojbSanPham->setAttribute('seq', $seqMin);
		        	$ojbSanPham->update(array('id' => $id));
		        	$ojbSanPham->emptyAttribute();
		        	$ojbSanPham->setAttribute('seq', $seq);
		        	$ojbSanPham->update(array('id' => $idMin));
		        }
		        
		    	// Down
		        if ($dataPost['mode'] == 'down') {
		        	$id = $dataPost['itemID'];
		        	$seq = $dataPost['type'];
		        	$seqMax = $ojbSanPham->getMinMax(array("seq"=>array("math"=>"<", "value"=>$seq), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm)), true);
		        	$idMax = $ojbSanPham->getIdBySeq($seqMax);
		        	
		        	$ojbSanPham->setAttribute('seq', $seqMax);
		        	$ojbSanPham->update(array('id' => $id));
		        	$ojbSanPham->emptyAttribute();
		        	$ojbSanPham->setAttribute('seq', $seq);
		        	$ojbSanPham->update(array('id' => $idMax));
		        }
        	} else {
		    	// Delete by id
		        if ($dataPost['mode'] == 'delete') {
		        	$dataSanpham = $ojbCategory->getByPrimaryKey($dataPost['itemID']);
		        	$ojbSCategory->delete(array('id' => $dataPost['itemID']));
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
				    FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
		        }
		        
		        // Delete all by array id
		    	if ($dataPost['mode'] == 'deleteall') {
		    		$dataPost = $dataPost['checkdel'];
		    		if (count ($dataPost) > 0) {
		    			foreach ($dataPost as $aP) {
		    				$dataSanpham = $ojbCategory->getByPrimaryKey($aP);
		        			$ojbSCategory->delete(array('id' => $aP));
		        			FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$dataSanpham['code']);
				    		FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$dataSanpham['code']);
		    			}
		    		}
		        }
		
		        // Up
		        if ($dataPost['mode'] == 'up') {
		        	$id = $dataPost['itemID'];
		        	$seq = $dataPost['type'];
		        	$seqMin = $ojbSCategory->getMinMax(array("iddm"=>array("math"=>"=", "value"=>$this->iddm), "seq"=>array("math"=>">", "value"=>$seq), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid)));
		        	$idMin = $ojbSCategory->getIdBySeq($seqMin);
		        	
		        	$ojbSCategory->setAttribute('seq', $seqMin);
		        	$ojbSCategory->update(array('id' => $id));
		        	$ojbSCategory->emptyAttribute();
		        	$ojbSCategory->setAttribute('seq', $seq);
		        	$ojbSCategory->update(array('id' => $idMin));
		        }
		        
		    	// Down
		        if ($dataPost['mode'] == 'down') {
		        	$id = $dataPost['itemID'];
		        	$seq = $dataPost['type'];
		        	$seqMax = $ojbSCategory->getMinMax(array("iddm"=>array("math"=>"=", "value"=>$this->iddm), "seq"=>array("math"=>"<", "value"=>$seq), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid)), true);
		        	$idMax = $ojbSCategory->getIdBySeq($seqMax);
		        	
		        	$ojbSCategory->setAttribute('seq', $seqMax);
		        	$ojbSCategory->update(array('id' => $id));
		        	$ojbSCategory->emptyAttribute();
		        	$ojbSCategory->setAttribute('seq', $seq);
		        	$ojbSCategory->update(array('id' => $idMax));
		        }
		        $this->dataPost = $dataPost;
        	}
        }
        
        $this->arrSCategory = array();
        $this->arrSanPham = array();
        if($this->iddm != '') {
        	if (!$ojbSCategory->checkTonTai($danhmucid, $this->iddm, true)) {
        		$this->maxseq = $ojbSCategory->getMinMax(array("iddm"=>array("math"=>"=", "value"=>$this->iddm), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid)), true);
        		$this->minseq = $ojbSCategory->getMinMax(array("iddm"=>array("math"=>"=", "value"=>$this->iddm), "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid)));
        		$this->arrSCategory = $ojbSCategory->getAllByAttribute(array("iddm"=>$this->iddm, "danhmuc_id"=>$danhmucid), array('id', 'iddm', 'seq', 'name'));
        		$this->flagAdd = true;
        		Session::Remove("CATE_SP");
        	} elseif (!$ojbSanPham->checkTonTai($danhmucid, $this->iddm)){
        		$this->maxseq = $ojbSanPham->getMinMax(array('danhmuc_id'=>array('math'=>'=', 'value'=>$danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm)), true);
        		$this->minseq = $ojbSanPham->getMinMax(array('danhmuc_id'=>array('math'=>'=', 'value'=>$danhmucid), "iddm"=>array("math"=>"=", "value"=>$this->iddm), "iddmc"=>array("math"=>"=", "value"=>$this->subiddm)));
        		
        		// List all
				$arrParam = array('m' => 'sanpham', 'iddm'=>$this->iddm, 'subiddm'=>$this->subiddm);
				$arrField = array('id', 'seq', 'iddm', 'iddmc', 'name', 'code', 'gia1', 'check_km', 'gia2', 'check_dis');
				$condition = array("iddm"=>array('value'=>$this->iddm, 'math'=>'='), 
								   "iddmc"=>array('value'=>$this->subiddm, 'math'=>'='), 
								   "danhmuc_id"=>array("math"=>"=", "value"=>$danhmucid));
        		$data = FunctionCommon::paging($ojbSanPham, $arrParam, $arrField, $condition);
	        	$arrContent = $data["data"];
				if (count ($arrContent) > 0) {
					foreach ($arrContent as $content) {
						$this->arrSanPham[] = $content;
					}
					$this->currPage = $data["currPage"];
					$this->numberPage = $data["numberPage"];
					$this->strPaging = $data["strPaging"];
				}
        		//$this->arrSanPham = $ojbSanPham->getAllByAttribute(array("iddm"=>$this->iddm, "danhmuc_id"=>$danhmucid, "iddmc"=>$this->subiddm), array('id', 'iddm', 'iddmc', 'seq', 'name'));
        		Session::Set("CATE_SP", 'true');
        	}
        }
        
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
    
	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new ViewAdmin();
        $this->tpl_title = '';
        $this->tpl_button = "Thêm mới";
        $this->tpl_title_list = '';
        $this->dialog = false;
        $this->id = '';
        $this->tenFile = '';

    	if (!$this->loginFlag || Session::Get("danhmucid") == '' || $this->dataUser['quyen'] > '1') {
        	FunctionCommon::sendRedirect();
        }
        $danhmucid = Session::Get("danhmucid");
        $this->iddm = isset($_GET['iddm']) ? $_GET['iddm'] : '';
        $this->dataPost = array('itemID'=>'', 'mode'=>'', 'type'=>'', 'name'=>'', 'nameimage'=>'', 'chitiet'=>'', 'huongdan'=>'', 'khuyenmai'=>'', 'hotro'=>'');
        $this->danhmucid = $danhmucid;
        
		$ojbSCategory = new SanphamSCategory();
        if ($this->isPost()) {
        	$this->dataPost = FunctionCommon::getPost(array_keys($this->dataPost));
        	$this->id = $this->dataPost['itemID'];
        	if ($this->dataPost['mode'] != '') {
        		// Update DB
        		if ($this->dataPost['type'] == 'edit' && $this->id != '') {
        			$ojbSCategory->setAttribute('name', $this->dataPost['name']);
	        		$ojbSCategory->setAttribute('code', $this->dataPost['nameimage']);
	        		$ojbSCategory->setAttribute('chitiet', $this->dataPost['chitiet']);
	        		$ojbSCategory->setAttribute('huongdan', $this->dataPost['huongdan']);
	        		$ojbSCategory->setAttribute('khuyenmai', $this->dataPost['khuyenmai']);
	        		$ojbSCategory->setAttribute('hotro', $this->dataPost['hotro']);
	        		$ojbSCategory->update(array('id' => $this->id));
	        		$this->dialog = true;
        		}
        		
        		// Insert DB
        		if ($this->dataPost['type'] == 'edit' && $this->id == '') {
        			$ojbSCategory->setAttribute('danhmuc_id', $danhmucid);
        			$ojbSCategory->setAttribute('seq', $ojbSCategory->getMinMax(array(), true) + 1);
		    		$ojbSCategory->setAttribute('iddm', $this->iddm);
		        	$ojbSCategory->setAttribute('name', $this->dataPost['name']);
		        	$ojbSCategory->setAttribute('code', $this->dataPost['nameimage']);
		        	$ojbSCategory->setAttribute('chitiet', $this->dataPost['chitiet']);
	        		$ojbSCategory->setAttribute('huongdan', $this->dataPost['huongdan']);
	        		$ojbSCategory->setAttribute('khuyenmai', $this->dataPost['khuyenmai']);
	        		$ojbSCategory->setAttribute('hotro', $this->dataPost['hotro']);
		        	$ojbSCategory->setAttribute('insert_at', date('Y-m-d H:i:s'));
		        	$ojbSCategory->insert();
		        	$this->dialog = true;
        		}
        		
        		// Xu ly file
	        	if ($this->dataPost['type'] == 'upload') {
	        		$this->tenFile = FunctionCommon::uploadFileTo($_FILES["code"], "img-the", $this->dataPost['nameimage']);
	        		if ($this->tenFile == false) {
	        			$this->tenFile = $this->dataPost['nameimage'];
	        		}
		        }
		        
		        // Delete image
	        	if ($this->dataPost['type'] == 'delete') {
		        	$tenFile = $this->dataPost['nameimage'];
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the/".$tenFile);
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/img-the-min/".$tenFile);
		        }
        	} else {
        		$this->tpl_button = "Sửa";
        		$this->dataPost = $ojbSCategory->getByPrimaryKey($this->id);
        		$this->tenFile = $this->dataPost['code'];
        	}
        }
        
        // Process view
        $objView->assignobj($this);
        $objView->display();
    }
}
?>

